package com.miya.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.miya.entity.MiyaResponse;
import com.miya.entity.PageRequest;
import com.miya.entity.PageResult;
import com.miya.entity.system.SystemUser;
import com.miya.system.model.AddUserDTO;
import com.miya.system.model.EditUserDTO;
import com.miya.system.service.IUserService;
import com.miya.utils.MiyaUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Caixiaowei
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2020年05月14日 10:18:00
 */
@RestController
@RequestMapping("/user")
@Api(tags = "系统用户管理")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * @title 分页查找用户信息
     * @description
     * @author Caixiaowei 
     * @updateTime 2020/5/14 10:21 
     * @throws 
     */
    @GetMapping("/list")
    public MiyaResponse findUserDetailPage(PageRequest request, SystemUser user) {
        PageResult<SystemUser> result = userService.list(user, request);
        return MiyaResponse.success(result);
    }

    /**
     * 查询当前登录用户
     *
     * @return {@link MiyaResponse}
     * @author Caixiaowei
     * @updateTime 2022/2/23 15:04
     */
    @GetMapping("/currentUser")
    public MiyaResponse currentUser() {
        Map<String, Object> map = Maps.newHashMap();
        map.put("currentUser", MiyaUtil.getCurrentUser());
        map.put("currentUsername", MiyaUtil.getCurrentUsername());
        map.put("currentUserAuthority", MiyaUtil.getCurrentUserAuthority());
        map.put("currentTokenValue", MiyaUtil.getCurrentTokenValue());
        map.put("currentRequestIpAddress", MiyaUtil.getHttpServletRequestIpAddress());
        return MiyaResponse.success(map);
    }

    @PostMapping("/新增用户")
    public MiyaResponse add(@RequestBody @Validated AddUserDTO dto) {
        return userService.add(dto);
    }

    @PostMapping("/修改用户")
    public MiyaResponse edit(@RequestBody @Validated EditUserDTO dto) {
        return userService.edit(dto);
    }
}
